package org.zp.handler;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zp.util.Captcha;

@RestController
public class CaptchaHandler {
	@GetMapping("captcha.act")
	public void captcha(HttpServletRequest req, HttpSession session ,HttpServletResponse resp) {
		System.out.println("验证码");
		//实例化验证码对象
		Captcha c = new Captcha();
		//拿到图片资源
		BufferedImage img = c.getImage();
		
		//拿到验证码的值
		String code = c.getCode();
		req.getSession().setAttribute("captcha", code);
		
		//拿到响应输出流
	    ServletOutputStream cs;
		try {
			cs = resp.getOutputStream();
			 //使用imgeIo输出图片
		    ImageIO.write(img, "jpeg", cs);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
		
	}
}
